/**
 * 商机主题维护
 * @author mengyb
 * @data 2019/04/11
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        element,
        vueData = {},
        table_where = {},
        cols = [[
            {type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'ROW_ID',title: '序号',width: 60, fixed: 'left',hide:true}
            , {field: 'GWBG001',title: '商机主题编号',width: 150, fixed: 'left',sort:true,
                search:'default',searchOpts:{icon:true}}
            , {field: 'GWBGL003',title: '说明',width: 80,sort:true,search:'default'}
            , {field: 'GWBGL004',title: '助记码',width: 80,search:'default'}
            , {field: 'GWBG002',title: '客户编号',width: 80,sort:true}
            , {field: 'GWBG002_NAME',title: '客户名称',width: 80,sort:true}
            , {field: 'GWBG005_NAME',title: '归属业务员',width: 80}
            , {field: 'GWBG003',title: '特价否',width: 80,filterDatas:app.datas_('base_gwat152_gwbg003')}
            , {field: 'GWBG004',title: '信用否',width: 80,filterDatas:app.datas_('base_gwat152_gwbg004')}
            , {field: 'GWBGACTI',title: '有效',width: 80,filterDatas:app.datas_('sys_y_n')}
            , {title: '', width: 60, toolbar: '#script-gwat152setting', fixed: 'right', align: 'center', unresize: true}
        ]],
        gwat152SettingAction,
        moduleName = '商机主题',
        editArea = ['900px', '380px']
    ;

    var Gwat152 = app.Module.extend({

        //初始化
        init: function () {
            self = this;
            self._super({
                _tag: 'gwat152',
                _searchs: [{
                    id: 'gwat152', tableId: 'gwat152_table', datas: cols[0]
                }],
                _loadTplsFull: [
                    {dom: 'gwat152_edit', tpl: 'base/gwat152_edit'},
                    {dom:'common_open_window',tpl:'common/_open_window'}

                ]
            }, win);
        },
        //初始化表格
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwat152_table'),
                    url: g_ctxPath + 'base/gwat152/getList',
                    method: 'post',
                    id: 'gwat152_table',
                    title: '商机主题表',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_where,
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwat152setting-action').innerHTML).render({}, function (string) {
                    gwat152SettingAction = string;
                });

                //弹出层索引
                var tipsIndex=null;


                //监听行工具栏事件，弹出编辑与删除按钮 lay-filter指定
                table.on('tool(gwat152_table)', function (obj) {
                    layer.tips(gwat152SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            tipsIndex = index;
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '1':
                                        self._openTplFull({
                                            tag: 'gwat152_edit',
                                            tagData: {
                                                isAdd:false,
                                                obj : obj,
                                                moduleName : moduleName
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: '修改'+moduleName+'信息',
                                                area:editArea
                                            }
                                        });
                                        break;
                                    case '2':
                                        self.delRow(obj);
                                        break;
                                }
                                layer.close(index);
                                tipsIndex = null;
                            });
                        }
                    });
                    return false;
                });

                //监听表头工具栏事件
                table.on('toolbar(gwat152_table)', function (obj) {
                    var checkStatus = table.checkStatus('gwat152_table');
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwat152_edit',
                                tagData: {
                                    isAdd: true,
                                    moduleName:moduleName
                                },
                                index: 0,
                                openOpts: {
                                    title: '新增'+moduleName+'信息',
                                    area:editArea
                                }
                            })
                            break;
                        case 'del':
                            self.delRows(checkStatus);
                            break;
                        case 'updateBatch':
                            self.updateBatch(checkStatus);
                            break;
                    }

                });

                //关闭弹出层
                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

                //监听行双击事件
                table.on('rowDouble(gwat152_table)',function (obj) {
                    self._openTplFull({
                        tag:'gwat152_edit',
                        tagData: {
                            obj:obj,
                            isAdd:false,
                            moduleName:moduleName,
                            is_detail:true
                        },
                        index:0,
                        openOpts:{
                            title: '修改'+moduleName+'信息',
                            area:editArea
                        }
                    })
                })

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwat152_table', where: table_where});
        },

        //删除
        delRow: function (obj) {
            app.confirm({
                msg:'确认删除该条信息吗？',
                ok: function () {
                    app.ajax({
                        xurl:'base/gwat152/delRow',
                        xdata:obj.data,
                        okmsg:moduleName+'信息删除成功',
                        loading:true,
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    })

                }
            });
        },
        //批量删除
        delRows: function (checkStatus) {
            if (checkStatus.data.length>0){//判断选中数据的个数
                var data=[];
                checkStatus.data.forEach(function(value){
                    var temp=app.fields_('base_gwat152');
                    temp.GWBGENT = value.GWBGENT;
                    temp.GWBG001 = value.GWBG001;
                    data.push(temp);
                });
                app.confirm({
                    msg:'确认删除所选择的信息吗？',
                    ok: function () {
                        app.ajax({
                            xurl:'base/gwat152/delRows',
                            xdata:data,
                            toJsonStr:true,
                            loading:true,
                            okmsg:moduleName+'信息删除成功',
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                }
                            }

                        })
                    }
                });
            } else{
                app.showMessage('请先选择待删除的信息！')
            }
        },
        //批量更改归属业务员
        updateBatch:function(checkStatus){
            if(checkStatus.data.length>0){
                var list=[];
                checkStatus.data.forEach(function(value){
                    var temp=app.fields_('base_gwat152');
                    temp.GWBGENT = value.GWBGENT;
                    temp.GWBG001 = value.GWBG001;
                    list.push(temp);
                });
                self._openTplFull({
                    tag:'common_open_ywy',
                    tagData:{
                        cb: function (data) {
                            app.ajax({
                                xurl:'base/gwat152/updateGwbg005',
                                xdata:{
                                    list:list, newGwbg005:data.ID
                                },
                                loading:true,
                                toJsonStr: true,
                                okmsg:'批量更改归属业务员成功！',
                                cb: function (result) {
                                    if (result.ok) {
                                        self.reloadTableCurr();
                                    }
                                }
                            });
                        }
                    },
                    index:1,
                    openOpts:{
                        title:'选择业务员',
                        area:['600px','430px']
                    }
                });
            }else{
                app.showMessage("请先选择待更改的信息");
            }

        },

        _ready: function () {
            vueData.serchData = table_where;
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    searchId: 'gwat152',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method:'q_gwbg001',
                                        columnList: [
                                            {label: '商机编号', prop: 'GWBG001', width: '150'},
                                            {label: '说明', prop: 'GWBGL003', width: '150'},
                                            {label: '助记码', prop: 'GWBGL004', width: '150'}
                                        ],
                                    },
                                    cb: function (data) {
                                        vue.isearch.fields.GWBG001 = data.GWBG001;
                                    }
                                },
                                index: 1,
                                openOpts: {
                                    title: '选择商机',
                                    area: ['600px', '465px']
                                }
                            });
                        }
                    }
                }]
            });
            self.initTable();
        },

        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwat152();

});